package com.longfor.dragonshard.service.base;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.longfor.dragonshard.entity.Demo;
import com.longfor.drogon.common.bean.LFDatatableExample;
import com.longfor.drogon.common.bean.LFDatatableResultBean;
import com.longfor.drogon.common.mybatis.LFMapper;
import com.longfor.drogon.common.service.IBaseService;
import com.longfor.drogon.common.util.LFDefaultKeyGenerator;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;
import tk.mybatis.mapper.common.Mapper;

import java.util.List;
import java.util.Optional;

/**
 * 基础Service的实现类，用于实现通用的基础Service方法
 *
 * @author chi.zhang
 * @create 2017/5/4 上午10:20
 *
 * @version v1.0
 **/
public abstract class AbstractBaseService<T> implements IBaseService<T> {
    @Autowired
    protected LFMapper<T> mapper;

    public LFMapper<T> getMapper() {
        return mapper;
    }

    @Override
    public Optional<T> selectOne(T entity) {
        return Optional.ofNullable(mapper.selectOne(entity));
    }

    /**
     * 根据多个id进行查询，此方法不使用缓存，不可随意使用
     *
     * @param ids e.g. 1,2,3
     * @return
     */
    @Override
    public List<T> selectByIds(String ids) {
        return mapper.selectByIds(ids);
    }

    /**
     * 这是批量新增方法的默认实现，实际开发中需要自己重写此方法
     * 此方法仅适用于非自增主键
     *
     * @param list
     * @return
     */
    @Override
    public Integer insertList(List<T> list) {
        return mapper.insertList(list);
    }

    @Override
    @SuppressWarnings("unchecked")
    public LFDatatableResultBean searchByExample(LFDatatableExample example) {
        PageHelper.startPage(example.getPageNum(), example.getPageSize(), true);
        List<T> list = mapper.selectByExample(example.getExample());
        return new LFDatatableResultBean(new PageInfo(list));
    }


}
